package com.example.timepicker;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.timecalculous.R;

import java.io.IOException;
import java.util.Calendar;

public class MusicActivity extends Activity {
    MediaPlayer mp;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); // hide title
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        winParams.flags |= (WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        mp =MediaPlayer.create(MusicActivity.this, R.raw.yingtixu);
        start();
        /* 当MediaPlayer.OnCompletionLister会运行的Listener */
        mp.setOnCompletionListener(
                new MediaPlayer.OnCompletionListener()
                {
                    // @Override
          /*覆盖文件播出完毕事件*/
                    public void onCompletion(MediaPlayer arg0)
                    {
                        try
                        {
              /*解除资源与MediaPlayer的赋值关系
               * 让资源可以为其它程序利用*/
                            mp.release();
              /*改变TextView为播放结束*/
                        }
                        catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                    }
                });

        /* 当MediaPlayer.OnErrorListener会运行的Listener */
        mp.setOnErrorListener(new MediaPlayer.OnErrorListener()
        {
            @Override
          /*覆盖错误处理事件*/
            public boolean onError(MediaPlayer arg0, int arg1, int arg2)
            {
                // TODO Auto-generated method stub
                try
                {
              /*发生错误时也解除资源与MediaPlayer的赋值*/
                    mp.release();
//                    tv.setText("播放发生异常!");
                }
                catch (Exception e)
                {
//                    tv.setText(e.toString());
                    e.printStackTrace();
                }
                return false;
            }
        });
        createDialog();
    }
    public void start(){
        mp =MediaPlayer.create(MusicActivity.this, R.raw.yingtixu);
        try {
            if(mp != null)
            {
                mp.stop();
            }
            mp.prepare();
            mp.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
private void createDialog() {
    new AlertDialog.Builder(this)
//                .setIcon(R.drawable.clock)
            .setTitle("闹钟提醒时间到了!!")
            .setMessage("你使用闹钟时间到了!!!")
            .setPositiveButton("推迟10分钟", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    tenMRemind();
                    mp.stop();
                    finish();
                }
            })
            .setNegativeButton("关闭", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    mp.stop();
                    finish();
                }
            }).show();
}
    /**
     * 推迟10分钟提醒
     */
    private void tenMRemind(){
        //设置时间
        Calendar calendar_now =Calendar.getInstance();


        calendar_now.setTimeInMillis(System.currentTimeMillis());
        calendar_now.set(Calendar.HOUR_OF_DAY, calendar_now.get(Calendar.HOUR_OF_DAY));
        calendar_now.set(Calendar.MINUTE, calendar_now.get(Calendar.MINUTE)+10);
        calendar_now.set(Calendar.SECOND, 0);
        calendar_now.set(Calendar.MILLISECOND, 0);

        //时间选择好了
        Intent intent = new Intent(this, CallAlarmReceiver.class);
        //注册闹钟广播
        PendingIntent sender = PendingIntent.getBroadcast(
                this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager am;
        am = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, calendar_now.getTimeInMillis(), sender);
    }
}
